import { DbService } from '@app/db';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
import { BaseController } from '../base/base.controller';
import { AddDto, UpdateDto } from './dto';
import { DtoPipe } from '../pipes/dto/dto.pipe';

@Controller('rechargeOption')
export class RechargeOptionController extends BaseController{
    constructor(
        private readonly db:DbService
    ){
        super()
    }

    @Get('list')
    async list() {
        const list = await this.db.rechargeOption.find()
        return this.successRes(list)
    }

    @Post('add')
    async add(@Body(new DtoPipe({skipMissingProperties : false})) dto:AddDto) {
        const res = await this.db.rechargeOption.insert(dto)
        return this.successRes(res.rows[0])
    }

    @Put('update/:id')
    async update(@Param('id', ParseIntPipe) id:number, @Body(new DtoPipe({skipMissingProperties : true})) dto:UpdateDto) {
        const res = await this.db.rechargeOption.update({id}, dto)
        return this.successRes()
    }

    @Delete('del/:id')
    async del(@Param('id', ParseIntPipe) id:number) {
        const res = await this.db.rechargeOption.delete({id})
        return this.successRes()
    }
}
